package com.pengtu.gsj.controller.yh;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pengtu.gsj.controller.base.DefaultBaseController;
import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.entity.yh.TecConEvalution;
import com.pengtu.gsj.interceptor.Token;
import com.pengtu.gsj.service.yh.TecconevalutionService;
import com.pengtu.gsj.utils.MathsUtils;
import com.pengtu.gsj.utils.web.SpringMvcUtils;

@Controller
@RequestMapping("tecConEvalut")
public class TecConEvalutionController extends DefaultBaseController{
	
	@Autowired
	private TecconevalutionService tecconevalutionService;
	
	@ModelAttribute
	public TecConEvalution loadEntity(String id) {
		TecConEvalution tecConEvalution = null;
		if (StringUtils.isNotEmpty(id)) {
			tecConEvalution = tecconevalutionService.getTecconevalution(id);
		} else {
			tecConEvalution = new TecConEvalution();
		}
		return tecConEvalution;
	}
	
	@Token(save = true)
	@RequestMapping("listInfo")
	public String showLisfInfo() {
		return "yh/tecConEvalut_list";
	}
	
	@ResponseBody
	@RequestMapping(value="list", produces = "application/json; charset=utf-8")
	public Page<TecConEvalution> getTecConEvalutionList(Page<TecConEvalution> page, HttpServletRequest request){
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(request);
		if (!page.isOrderBySetted()) {
			page.setOrderBy("testYear,createDate");
			page.setOrder("asc,desc");
		}
		page = tecconevalutionService.findTecconevalutions(page, queryFilter);
		return page;
	}
	
	@ResponseBody
	@Token(remove = true)
	@RequestMapping(value = "save", method=RequestMethod.POST)
	public boolean saveTecConEvalution(@ModelAttribute TecConEvalution tecConEvalution) {
		tecConEvalution.setAutocheckRatio(MathsUtils.divDouble(tecConEvalution.getAutomaticDetectionMileage()*100, (tecConEvalution.getMileageAssessed() - tecConEvalution.getConstructionMileage()), 2));
		tecconevalutionService.saveTecconevalution(tecConEvalution);
		return true;
	}
	
	@ResponseBody  
	@RequestMapping(value="show")
	public TecConEvalution showTecConEvalution (@ModelAttribute TecConEvalution tecEvalution){
		return tecEvalution;
	}
	
	@ResponseBody
	@RequestMapping(value="delete")
	public boolean deleteTecConEvalution(@ModelAttribute TecConEvalution tecConEvalution){
		tecconevalutionService.deleteTecconevalution(tecConEvalution);
		return true;
	}
	
	@ResponseBody
	@RequestMapping("checkTestYear")
	public boolean checkTestYear(){
		String testYear = SpringMvcUtils.getParameter("testYear");
		String id = SpringMvcUtils.getParameter("id");
		return tecconevalutionService.checkTestYear(testYear, id);
	}

}
